﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmAwardInfo : Form
    {
        EmployeeClient _wcfEmployee;
        EmployeeInfo _EmployeeInfo;
        AwardClient _wcfAward;
        AwardInfo _AwardInfo;
        AwardExpInfo _AwardExpInfo;
        DialogResult dlgResult;
        RoleRightInfo listRole;
        bool isChange = false;
        public frmAwardInfo(int AwardID, int employeeid)
        {
            InitializeComponent();
            //LoadLanguage();
            UISetting.NapLoaiKhenThuong(ref cboType);
            listRole = UIParamater.Quyen_KhenThuong;
            _wcfAward = Service.Award();
            _wcfEmployee = Service.Employee();
            _AwardInfo = new AwardInfo();
            _EmployeeInfo = new EmployeeInfo();
            _AwardExpInfo = new AwardExpInfo();

            dlgResult = DialogResult.Cancel;
            txtMisDate.Text = DateTime.Today.ToString("dd/MM/yyyy");
            if (employeeid > 0)
            {
                GetEmployeeName(employeeid);
                btnSelectEmp.Visible = false;
            }
            else
                btnSelectEmp.Visible = true;
            if (!listRole._create)
                btnNew.Enabled = false;
            if (!listRole._update)
                btnSave.Enabled = false;
            if (AwardID > 0)
            {
                _AwardInfo = _wcfAward.Award_GetInfo(AwardID);
                Info2Form();
                GetEmployeeName(_AwardInfo.EmployeeID);
                AwardExpOut();
            }
            _AwardInfo.ID = AwardID;
            isChange = false;
        }
        void GetEmployeeName(int employeeid)
        {
            _EmployeeInfo = _wcfEmployee.Employee_GetInfo(employeeid);
            txtEmpName.Tag = _EmployeeInfo.ID;
            txtEmpName.Text = _EmployeeInfo.LastName + " " + _EmployeeInfo.FirstName;
        }
        public void LoadLanguage()
        {
            this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_Panel(ref panel2);
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_GroupBox(ref grbMiscondcut);
        }

        private void Info2Form()//Hiển thị thông tin ra form
        {
            txtNo.Text = _AwardInfo.No;
            txtMisDate.Text = _AwardInfo.Date.ToString("dd/MM/yyyy");
            txtContent.Text = _AwardInfo.Detail;
            txtTypeContent.Text = _AwardInfo.TypeContent;
            cboType.SelectedValue = _AwardInfo.Type;
            txtAmount.Text = _AwardInfo.Amount.ToString("#,##0");
            isChange = false;
        }

        private void Form2Info()//Chuyển dữ liệu từ form nhập vào Info
        {
            _AwardInfo.EmployeeID = Convert.ToInt32(txtEmpName.Tag);
            _AwardInfo.No = txtNo.Text;
            _AwardInfo.Detail = txtContent.Text;
            _AwardInfo.Date = DateTime.ParseExact(txtMisDate.Text, "dd/MM/yyyy", null);
            _AwardInfo.TypeContent = txtTypeContent.Text;
            _AwardInfo.Type = cboType.SelectedValue.ToString();
            _AwardInfo.Amount = UISetting.GetDecimalNo(txtAmount.Text);
        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            DateTime DateVal;
            if (Convert.ToInt32(txtEmpName.Tag) == 0)
            {
                txtEmpName.Focus();
                MessageBox.Show(UpdateUI.GetString("chuachonnhanvien"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!DateTime.TryParseExact(txtMisDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateVal))
            {
                txtMisDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngaynhankhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (_AwardInfo.ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            _AwardInfo.ID = 0;
            _AwardInfo = new AwardInfo();
            txtMisDate.Text = DateTime.Today.ToString("dd/MM/yyyy");
            txtContent.Text = "";
            txtEmpName.Text = "";
            txtEmpName.Tag = 0;
            cboType.SelectedValue = "KT";
            txtAmount.Text = "0";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            Form2Info();
            if (_AwardInfo.ID > 0)//update
            {
                if (!listRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                AwardExp_Save();
                if (_wcfAward.Award_Update(_AwardInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else//insert
            {
                if (!listRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _AwardInfo.ID = _wcfAward.Award_Insert(_AwardInfo);
                if (_AwardInfo.ID > 0)
                {
                    AwardExp_Save();
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
        }

        #region ReadMisConductToInfo
        //Nếu muốn thêm 1 Mis vào form thì đồng nghĩa với việc thêm 1 AwardExpInfo sau
        AwardExpInfo ReadMis1()
        {
            if (_AwardInfo.ID > 0)
            {
                AwardExpInfo info = new AwardExpInfo();
                info.ID = 0;
                info.AwardID = _AwardInfo.ID;
                if (chk1.Checked)
                    info.MisconductID = 1;
                else
                    info.MisconductID = 0;
                return info;
            }
            else
                return null;
        }
        AwardExpInfo ReadMis2()
        {
            if (_AwardInfo.ID > 0)
            {
                AwardExpInfo info = new AwardExpInfo();
                info.ID = 0;
                info.AwardID = _AwardInfo.ID;
                if (chk2.Checked)
                    info.MisconductID = 2;
                else
                    info.MisconductID = 0;
                return info;
            }
            else
                return null;
        }
        AwardExpInfo ReadMis3()
        {
            if (_AwardInfo.ID > 0)
            {
                AwardExpInfo info = new AwardExpInfo();
                info.ID = 0;
                info.AwardID = _AwardInfo.ID;
                if (chk3.Checked)
                    info.MisconductID = 3;
                else
                    info.MisconductID = 0;
                return info;
            }
            else
                return null;
        }
        AwardExpInfo ReadMis4()
        {
            if (_AwardInfo.ID > 0)
            {
                AwardExpInfo info = new AwardExpInfo();
                info.ID = 0;
                info.AwardID = _AwardInfo.ID;
                if (chk4.Checked)
                    info.MisconductID = 4;
                else
                    info.MisconductID = 0;
                return info;
            }
            else
                return null;
        }
        AwardExpInfo ReadMis5()
        {
            if (_AwardInfo.ID > 0)
            {
                AwardExpInfo info = new AwardExpInfo();
                info.ID = 0;
                info.AwardID = _AwardInfo.ID;
                if (chk5.Checked)
                    info.MisconductID = 5;
                else
                    info.MisconductID = 0;
                return info;
            }
            else
                return null;
        }
        AwardExpInfo ReadMis6()
        {
            if (_AwardInfo.ID > 0)
            {
                AwardExpInfo info = new AwardExpInfo();
                info.ID = 0;
                info.AwardID = _AwardInfo.ID;
                if (chk6.Checked)
                    info.MisconductID = 6;
                else
                    info.MisconductID = 0;
                return info;
            }
            else
                return null;
        }
        AwardExpInfo ReadMis7()
        {
            if (_AwardInfo.ID > 0)
            {
                AwardExpInfo info = new AwardExpInfo();
                info.ID = 0;
                info.AwardID = _AwardInfo.ID;
                if (chk7.Checked)
                    info.MisconductID = 7;
                else
                    info.MisconductID = 0;
                return info;
            }
            else
                return null;
        }
        AwardExpInfo ReadMis8()
        {
            if (_AwardInfo.ID > 0)
            {
                AwardExpInfo info = new AwardExpInfo();
                info.ID = 0;
                info.AwardID = _AwardInfo.ID;
                if (chk8.Checked)
                    info.MisconductID = 8;
                else
                    info.MisconductID = 0;
                return info;
            }
            else
                return null;
        }
        AwardExpInfo ReadMis9()
        {
            if (_AwardInfo.ID > 0)
            {
                AwardExpInfo info = new AwardExpInfo();
                info.ID = 0;
                info.AwardID = _AwardInfo.ID;
                if (chk9.Checked)
                    info.MisconductID = 9;
                else
                    info.MisconductID = 0;
                return info;
            }
            else
                return null;
        }
        AwardExpInfo ReadMis10()
        {
            if (_AwardInfo.ID > 0)
            {
                AwardExpInfo info = new AwardExpInfo();
                info.ID = 0;
                info.AwardID = _AwardInfo.ID;
                if (chk10.Checked)
                    info.MisconductID = 10;
                else
                    info.MisconductID = 0;
                return info;
            }
            else
                return null;
        }
        AwardExpInfo ReadMis11()
        {
            if (_AwardInfo.ID > 0)
            {
                AwardExpInfo info = new AwardExpInfo();
                info.ID = 0;
                info.AwardID = _AwardInfo.ID;
                if (chk11.Checked)
                    info.MisconductID = 11;
                else
                    info.MisconductID = 0;
                return info;
            }
            else
                return null;
        }
        AwardExpInfo ReadMis12()
        {
            if (_AwardInfo.ID > 0)
            {
                AwardExpInfo info = new AwardExpInfo();
                info.ID = 0;
                info.AwardID = _AwardInfo.ID;
                if (chk12.Checked)
                    info.MisconductID = 12;
                else
                    info.MisconductID = 0;
                return info;
            }
            else
                return null;
        }
        AwardExpInfo ReadMis13()
        {
            if (_AwardInfo.ID > 0)
            {
                AwardExpInfo info = new AwardExpInfo();
                info.ID = 0;
                info.AwardID = _AwardInfo.ID;
                if (chk13.Checked)
                    info.MisconductID = 13;
                else
                    info.MisconductID = 0;
                return info;
            }
            else
                return null;
        }
        #endregion

        private void AwardExp_Save()
        {
            try
            {
                _wcfAward.AwardExp_Delete(_AwardInfo.ID);
                if (cboType.SelectedIndex != 0)
                {
                    AwardExpInfo info1 = new AwardExpInfo();
                    info1 = ReadMis1();
                    if (info1.MisconductID > 0)
                    {
                        if (_wcfAward.AwardExp_Insert(info1) == -1)
                        {
                            MessageBox.Show(UpdateUI.GetString("taomoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }

                    AwardExpInfo info2 = new AwardExpInfo();
                    info2 = ReadMis2();
                    if (info2.MisconductID > 0)
                    {
                        if (_wcfAward.AwardExp_Insert(info2) == -1)
                        {
                            MessageBox.Show(UpdateUI.GetString("taomoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }

                    AwardExpInfo info3 = new AwardExpInfo();
                    info3 = ReadMis3();
                    if (info3.MisconductID > 0)
                    {
                        if (_wcfAward.AwardExp_Insert(info3) == -1)
                        {
                            MessageBox.Show(UpdateUI.GetString("taomoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }

                    AwardExpInfo info4 = new AwardExpInfo();
                    info4 = ReadMis4();
                    if (info4.MisconductID > 0)
                    {
                        if (_wcfAward.AwardExp_Insert(info4) == -1)
                        {
                            MessageBox.Show(UpdateUI.GetString("taomoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }

                    AwardExpInfo info5 = new AwardExpInfo();
                    info5 = ReadMis5();
                    if (info5.MisconductID > 0)
                    {
                        if (_wcfAward.AwardExp_Insert(info5) == -1)
                        {
                            MessageBox.Show(UpdateUI.GetString("taomoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }

                    AwardExpInfo info6 = new AwardExpInfo();
                    info6 = ReadMis6();
                    if (info6.MisconductID > 0)
                    {
                        if (_wcfAward.AwardExp_Insert(info6) == -1)
                        {
                            MessageBox.Show(UpdateUI.GetString("taomoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }

                    AwardExpInfo info7 = new AwardExpInfo();
                    info7 = ReadMis7();
                    if (info7.MisconductID > 0)
                    {
                        if (_wcfAward.AwardExp_Insert(info7) == -1)
                        {
                            MessageBox.Show(UpdateUI.GetString("taomoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }

                    AwardExpInfo info8 = new AwardExpInfo();
                    info8 = ReadMis8();
                    if (info8.MisconductID > 0)
                    {
                        if (_wcfAward.AwardExp_Insert(info8) == -1)
                        {
                            MessageBox.Show(UpdateUI.GetString("taomoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }

                    AwardExpInfo info9 = new AwardExpInfo();
                    info9 = ReadMis9();
                    if (info9.MisconductID > 0)
                    {
                        if (_wcfAward.AwardExp_Insert(info9) == -1)
                        {
                            MessageBox.Show(UpdateUI.GetString("taomoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }

                    AwardExpInfo info10 = new AwardExpInfo();
                    info10 = ReadMis10();
                    if (info10.MisconductID > 0)
                    {
                        if (_wcfAward.AwardExp_Insert(info10) == -1)
                        {
                            MessageBox.Show(UpdateUI.GetString("taomoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }

                    AwardExpInfo info11 = new AwardExpInfo();
                    info11 = ReadMis11();
                    if (info11.MisconductID > 0)
                    {
                        if (_wcfAward.AwardExp_Insert(info11) == -1)
                        {
                            MessageBox.Show(UpdateUI.GetString("taomoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }

                    AwardExpInfo info12 = new AwardExpInfo();
                    info12 = ReadMis12();
                    if (info12.MisconductID > 0)
                    {
                        if (_wcfAward.AwardExp_Insert(info12) == -1)
                        {
                            MessageBox.Show(UpdateUI.GetString("taomoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }

                    AwardExpInfo info13 = new AwardExpInfo();
                    info13 = ReadMis13();
                    if (info13.MisconductID > 0)
                    {
                        if (_wcfAward.AwardExp_Insert(info13) == -1)
                        {
                            MessageBox.Show(UpdateUI.GetString("taomoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n" + ex.Message + "\n" + UpdateUI.GetString("ghiloi"));
            }
        }

        private void AwardExpOut()
        {
            try
            {
                DataTable dt = _wcfAward.AwardExp_GetByAwardID(_AwardInfo.ID).Tables[0];
                foreach (DataRow item in dt.Rows)
                {
                    switch (Convert.ToInt32(item["MisconductID"]))
                    {
                        case 1:
                            chk1.Checked = true;
                            break;
                        case 2:
                            chk2.Checked = true;
                            break;
                        case 3:
                            chk3.Checked = true;
                            break;
                        case 4:
                            chk4.Checked = true;
                            break;
                        case 5:
                            chk5.Checked = true;
                            break;
                        case 6:
                            chk6.Checked = true;
                            break;
                        case 7:
                            chk7.Checked = true;
                            break;
                        case 8:
                            chk8.Checked = true;
                            break;
                        case 9:
                            chk9.Checked = true;
                            break;
                        case 10:
                            chk10.Checked = true;
                            break;
                        case 11:
                            chk11.Checked = true;
                            break;
                        case 12:
                            chk12.Checked = true;
                            break;
                        case 13:
                            chk13.Checked = true;
                            break;
                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n" + ex.Message + "\n" + UpdateUI.GetString("ghiloi"));
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmAward_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            _wcfAward.Close();
            _wcfEmployee.Close();

            this.DialogResult = dlgResult;
        }

        private void frmAward_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }
        }

        private void txtAmount_Leave(object sender, EventArgs e)
        {
            txtAmount.Text = UISetting.GetDecimalNo(txtAmount.Text).ToString("#,##0");
        }

        private void cboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboType.SelectedIndex == 0)
            {
                grbMiscondcut.Enabled = false;
                chk1.Checked
                    = chk2.Checked
                    = chk3.Checked
                    = chk4.Checked
                    = chk5.Checked
                    = chk6.Checked
                    = chk7.Checked
                    = chk8.Checked
                    = chk9.Checked
                    = chk10.Checked
                    = chk11.Checked
                    = chk12.Checked
                    = chk13.Checked
                    = false;
            }
            else
            {
                grbMiscondcut.Enabled = true;
            }
        }

        private void btnSelectEmp_Click(object sender, EventArgs e)
        {
            frmEmployeeList f = new frmEmployeeList(true);
            if (f.ShowDialog() == DialogResult.OK)
            {
                GetEmployeeName(f.EmpID);
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (_AwardInfo.ID > 0)
            {
                if (_AwardInfo.Type == "KL")
                {
                    frmPreviewReport f = new frmPreviewReport("AwardMisconduct", new string[] { _AwardInfo.ID.ToString(), cboType.SelectedValue.ToString() });
                    f.ShowDialog();
                }
            }
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }

        private void fullName_Click(object sender, EventArgs e)
        {

        }

    }
}
